package zhoufu.sz.sanxiao_check.ui.basic;

import android.content.Intent;
import android.databinding.ViewDataBinding;
import android.support.annotation.Nullable;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import javax.inject.Inject;
import zhoufu.sz.sanxiao_check.R;
import zhoufu.sz.sanxiao_check.databinding.ActivityBasicItem2Binding;
import zhoufu.sz.sanxiao_check.inject.components.DaggerLoginComponet;
import zhoufu.sz.sanxiao_check.inject.components.LoginComponet;
import zhoufu.sz.sanxiao_check.models.FireNoticeModel;
import zhoufu.sz.sanxiao_check.presents.BasicItem2Presenter;
import zhoufu.sz.sanxiao_check.ui.GestureViewActivity;
import zhoufu.sz.sanxiao_check.ui.contract.BasicMsgItem2Contract;
import zhoufu.sz.sanxiao_check.utils.UIUtils;

/**
 * User:RCP
 * email:ruancaipu@gmail.com
 * Date:2016/7/26
 * 消防安全告知书
 */
public class BasicItem2Activity extends BaseMsgActivity implements View.OnClickListener,
        BasicMsgItem2Contract.View<FireNoticeModel>
{
    @Inject
    BasicItem2Presenter _presenter;

    ActivityBasicItem2Binding _binding;

    @Override protected void initInject()
    {
        setTitle(R.string.title_basic_item2);
        LoginComponet loginComponet = DaggerLoginComponet.builder()
                .activityModule(getActivityModule()).appComponent(getAppComponent()).build();
        loginComponet.inject(this);
        _presenter.bindView(this);
        _presenter.onStart();
    }

    @Override protected void initAfterOnCreate(ViewDataBinding binding)
    {
        super.initAfterOnCreate(binding);
        _binding = (ActivityBasicItem2Binding) binding;
        _binding.setOnClickListener(this);
    }

    @Nullable @Override protected android.databinding.DataBindingComponent setBindingComponent()
    {
        return LoginComponet.Instance.get();
    }

    @Override protected int setIndividualId()
    {
        return R.layout.activity_basic_item2;
    }


    @Override public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.fireselfsignimg:
                startActivityForResult(new Intent(this, GestureViewActivity.class),
                        0);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        if ( data != null )
        {
            String img = data.getStringExtra(GestureViewActivity.RESULT_DATA);
            _presenter.setSignPath(img);
            _binding.setUrl(img);
        }
    }

    @Override public String SID()
    {
        return mInfoModel.getSID();
    }

    @Override public void setModel(FireNoticeModel fireNoticeModel)
    {
        _binding.setUrl(fireNoticeModel.getDuty());
        _presenter.setSignPath(fireNoticeModel.getDuty());
    }

    @Override public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.menu_save, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override public boolean onOptionsItemSelected(MenuItem item)
    {
        if ( item.getItemId() == R.id.menu_save )
        {
            _presenter.update();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    @Override public void updateSuccese()
    {
        UIUtils.showToast(this, getString(R.string.save_succese));
        finish();
    }

    @Override public void showToast(String msg)
    {
        UIUtils.showSnackbar(_binding.getRoot(), msg);
    }

    @Override public void pullFaild()
    {
        hideProgreesDialog();
        UIUtils.showSnackbar(_binding.getRoot(), R.string.toast_network_error);
    }

    @Override public void pullEnd()
    {
        hideProgreesDialog();
    }

    @Override public void pullStart()
    {
        showProgreesDialog();
    }
}
